package com.shoulder.boot.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.system.dto.DeptAddDTO;
import com.shoulder.boot.system.dto.DeptModDTO;
import com.shoulder.boot.system.dto.DeptQueryDTO;
import com.shoulder.boot.system.entity.Dept;
import com.shoulder.boot.system.exception.SystemException;
import com.shoulder.boot.system.mapper.DeptMapper;
import com.shoulder.boot.system.service.DeptService;
import com.shoulder.boot.system.utils.DeptUtils;
import com.shoulder.boot.system.vo.DeptVo;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.utils.TokenUtils;
import com.shoulder.core.utils.TreeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 部门信息 服务实现类
 *
 * @author 596767880@qq.com
 * @since 2024-04-01 16:20:54
 */
@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    @Override
    public List<DeptVo> listQuery(DeptQueryDTO queryDTO) {
        List<Dept> deptList = this.baseMapper.selectList(DeptUtils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(deptList)) {
            return Collections.emptyList();
        }
        return DeptUtils.buildUtils().toVos(deptList);
    }

    @Override
    public DeptVo look(String id) {
        Dept entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("部门信息不存在");
        }

        return DeptUtils.buildUtils().toVo(entity);
    }

    @Override
    public List<DeptVo> looks(BaseIds<String> baseIds) {
        List<Dept> deptList = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(deptList)) {
            return Collections.emptyList();
        }
        return DeptUtils.buildUtils().toVos(deptList);
    }

    @Override
    public void del(String id) {
        Dept entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("部门信息不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<DeptVo> pageQuery(DeptQueryDTO queryDTO) {
        Page<Dept> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
                , DeptUtils.buildQueryWrapper(queryDTO));
        if (selectPage == null || selectPage.getRecords() == null || selectPage.getRecords().isEmpty()) {
            return new Page<>();
        }
        return DeptUtils.buildUtils().toPage(selectPage);
    }

    @Override
    public void mod(DeptModDTO modDTO) {
        Dept info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new SystemException("部门信息不存在"));
        Dept dept = this.baseMapper.selectOne(new LambdaQueryWrapper<Dept>().eq(Dept::getName, modDTO.getName())
                .ne(Dept::getId, modDTO.getId()).eq(Dept::getTenantId, TokenUtils.parseUser().getTenantId()));
        if (dept != null) {
            throw new SystemException("部门名称已存在");
        }
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(DeptAddDTO addDTO) {
        Dept info = this.baseMapper.selectOne(new LambdaQueryWrapper<Dept>().eq(Dept::getName, addDTO.getName())
                .eq(Dept::getTenantId, TokenUtils.parseUser().getTenantId()));
        if (info != null) {
            throw new SystemException("部门名称已存在");
        }
        info = new Dept();
        BeanUtils.copyProperties(addDTO, info);
        info.setTenantId(TokenUtils.parseUser().getTenantId());
        this.baseMapper.insert(info);
    }

    @Override
    public List<Dept> tree() {
        List<Dept> deptList = this.baseMapper.selectList(new LambdaQueryWrapper<Dept>().eq(Dept::getTenantId, TokenUtils.parseUser().getTenantId()));
        //return RecursionUtils.toList(MapUtils.bean2Map(deptList), Dept.class);
        return TreeUtils.build(deptList, Dept::getParentId, Dept::getId, e -> "0".equals(e.getParentId()), Dept::setChildren);
    }

}